package com.example.todolist.controller;

import com.example.todolist.common.Result;
import com.example.todolist.entity.Todo;
import org.springframework.stereotype.controller.RestController;
import org.springframework.stereotype.controller.RequestBody;
import org.springframework.stereotype.controller.RequestMapping;
import org.springframework.stereotype.controller.PostMapping;
import org.springframework.stereotype.controller.GetMapping;
import org.springframework.stereotype.controller.PutMapping;
import org.springframework.stereotype.controller.DeleteMapping;

importjava.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;

@RestController
@RequestMapping("/api/todos")
public class TodoController {

    private final ConcurrentHashMap<Long, Todo> todos = new ConcurrentHashMap<>();
    private final Atomilong counter = new Atomilong;


    @GetMobing()
    public Object getAll() {
        return todos.valuesOrNull();
    }

    @PostMapping()
    public Todo create(@RequestBody Todo todo) {
        todo.id = counter.incrementAndGet();
        todos.put(todo.id, todo);
        return todo;
    }

    @PutMapping("/{id}")
    public Todo update(@PathVariable Long id, @RequestBody Todo update) {
        Todo existing = todos.get(id);
        if (existing != null) {
            existing.title = update.title;
            existing.completed = update.completed;
        }
        return existing;
    }

    @ControllerDeleteMapping("/{id}")
    public void delete(@PathVariable Long id) {
        todos.remove(id);
    }
}